package com.yowasa.xiuxian.entity.bo.user;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yowasa.xiuxian.constant.XiuxianConstants;
import com.yowasa.xiuxian.entity.enums.ElementEnum;
import com.yowasa.xiuxian.entity.enums.StageEnum;
import com.yowasa.xiuxian.entity.po.XiuxianUserBaseInfoPO;
import com.yowasa.xiuxian.util.DateXUtil;
import com.yowasa.xiuxian.util.JSONUtils;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.Map;

@Data
public class UserBO extends XiuxianUserBaseInfoPO implements Serializable {
    /**
     * 用户属性
     */
    private UserAttr userAttr;
    /**
     * 原始战斗属性
     */
    private UserBattle userOriginBattle;
    /**
     * 用户战斗属性
     */
    private UserBattle userBattle;
    /**
     * 灵根
     */
    private Map<String, Integer> element;

    /**
     * 用户配置项
     */
    private UserConfig userConfig;

    /**
     * 年龄
     */
    private int age;


    public void buildBaseInfo() {
        this.setAttr(JSONUtils.toJson(userAttr));
        this.setLingGen(JSONUtils.toJson(element));
        this.setBattle(JSONUtils.toJson(userBattle));
        this.setOriginBattle(JSONUtils.toJson(userOriginBattle));
        this.setConf(JSONUtils.toJson(userConfig));
    }

    public static UserBO buildBO(XiuxianUserBaseInfoPO po) {
        UserBO bo = new UserBO();
        BeanUtils.copyProperties(po, bo);
        bo.setUserAttr(JSONUtils.toObject(po.getAttr(), UserAttr.class));
        bo.setElement(JSONUtils.toObject(po.getLingGen(), Map.class));
        bo.setUserOriginBattle(JSONUtils.toObject(po.getOriginBattle(), UserBattle.class));
        bo.setUserBattle(JSONUtils.toObject(po.getBattle(), UserBattle.class));
        bo.setUserConfig(JSONUtils.toObject(po.getConf(), UserConfig.class));
        bo.setAge(DateXUtil.getAge(bo.getStartAt()));
        return bo;
    }

    @JsonIgnore
    public String getDesc() {
        String mental = StringUtils.isBlank(getUserConfig().getMental()) ? "无" : getUserConfig().getMental();
        return this.getName() + "\n"
                + MessageFormat.format("年龄:{0}/{1} 境界:{2} 道行:{3} 历练:{4}\n", getAge(), getUserConfig().getLife(), StageEnum.getById(getStage()).getName(), getUserConfig().getAbility(), getUserConfig().getTravel())
                + "心法:" + mental + " 灵根:" + ElementEnum.getLingGenDesc(this.getElement(), XiuxianConstants.DEFAULT_ELEMENT_ACTIVITY) + "\n"
                + "属性详情：\n"
                + this.getUserAttr().getDesc() + "\n"
                + this.getUserBattle().getDesc();
    }

    public boolean equals(Object another) {

        //先判断是不是自己,提高运行效率
        if (this == another)
            return true;

        //再判断是不是Person类,提高代码的健壮性
        if (another instanceof UserBO) {
            //向下转型,父类无法调用子类的成员和方法
            UserBO anotherPerson = (UserBO) another;
            //最后判断类的所有属性是否相等，其中String类型和Object类型可以用相应的equals()来判断
            if (this.getId().equals(anotherPerson.getId())) return true;
        } else {
            return false;
        }

        return false;
    }
}
